using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using UwinEducation.API.Attributes;
using UwinEducation.Application.Common.Interfaces.Orders;
using UwinEducation.Application.Dtos.Orders;
using UwinEducation.Shared.Models;

namespace UwinEducation.API.Controllers
{
    /// <summary>
    /// 订单详情管理控制器
    /// </summary>
    [Authorize]
    //[RequirePermission("student.order")] // 指定所需的权限
    [ApiController]
    [Route("api/[controller]/[action]")]
    public class OrderDetailController : ControllerBase
    {
        private readonly IOrderDetailService _orderDetailService;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="orderDetailService">订单详情服务</param>
        public OrderDetailController(IOrderDetailService orderDetailService)
        {
            _orderDetailService = orderDetailService;
        }

        /// <summary>
        /// 获取订单详情
        /// </summary>
        /// <param name="request">订单详情ID请求参数</param>
        /// <returns>订单详情</returns>
        [HttpPost]
        public async Task<ApiResponse<OrderDetailDto>> GetById([FromBody] IdRequest request)
        {
            var orderDetail = await _orderDetailService.GetByIdAsync(request.Id);
            return ApiResponse<OrderDetailDto>.Ok(orderDetail);
        }

        /// <summary>
        /// 获取订单的所有详情
        /// </summary>
        /// <param name="request">订单ID请求参数</param>
        /// <returns>订单详情列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<OrderDetailDto>>> GetByOrderId([FromBody] IdRequest request)
        {
            var orderDetails = await _orderDetailService.GetByOrderIdAsync(request.Id);
            return ApiResponse<List<OrderDetailDto>>.Ok(orderDetails);
        }

        /// <summary>
        /// 获取教师课表关联的订单详情
        /// </summary>
        /// <param name="request">教师课表ID请求参数</param>
        /// <returns>订单详情列表</returns>
        [HttpPost]
        public async Task<ApiResponse<List<OrderDetailDto>>> GetByTeacherScheduleId([FromBody] IdRequest request)
        {
            var orderDetails = await _orderDetailService.GetByTeacherScheduleIdAsync(request.Id);
            return ApiResponse<List<OrderDetailDto>>.Ok(orderDetails);
        }

        /// <summary>
        /// 添加订单详情
        /// </summary>
        /// <param name="request">添加订单详情请求参数</param>
        /// <returns>添加的订单详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<OrderDetailDto>> Add([FromBody] CreateOrderDetailDto request)
        {
            var orderDetail = await _orderDetailService.AddAsync(request, request.OrderId);
            return ApiResponse<OrderDetailDto>.Ok(orderDetail);
        }

        /// <summary>
        /// 更新订单详情
        /// </summary>
        /// <param name="orderDetailDto">更新订单详情请求参数</param>
        /// <returns>更新后的订单详情</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<OrderDetailDto>> Update([FromBody] UpdateOrderDetailDto orderDetailDto)
        {
            var orderDetail = await _orderDetailService.UpdateAsync(orderDetailDto);
            return ApiResponse<OrderDetailDto>.Ok(orderDetail);
        }

        /// <summary>
        /// 删除订单详情
        /// </summary>
        /// <param name="request">订单详情ID请求参数</param>
        /// <returns>操作结果</returns>
        [HttpPost]
        [RequirePermission("student.order")] // 指定所需的权限
        public async Task<ApiResponse<object>> Delete([FromBody] IdRequest request)
        {
            await _orderDetailService.DeleteAsync(request.Id);
            return ApiResponse<object>.Ok(new object());
        }

        /// <summary>
        /// 分页查询订单详情
        /// </summary>
        /// <param name="request">分页查询请求参数</param>
        /// <returns>分页查询结果</returns>
        [HttpPost]
        public async Task<ApiResponse<PagedResult<OrderDetailDto>>> GetPaged([FromBody] OrderDetailPageRequest request)
        {
            var orderDetails = await _orderDetailService.GetPagedAsync(request);
            return ApiResponse<PagedResult<OrderDetailDto>>.Ok(orderDetails);
        }
    }
} 